# 作用：把提取出来的GT可视化，看看是不是转错了
import cv2
# from os import getcwd



classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
DATA_PATH = "D:/Code/ML-vsc/6-27-yolo3/VOCdevkit/VOC2007/JPEGImages"

# imgname是输入图像的名称，无下标
def show_labels_img(imgname):
    img = cv2.imread(f"JPEGImages/{imgname}.jpg")
    # print(img)
    # print(getcwd())
    with open("../../2007_train.txt",'r') as f:
        img_path = f"{DATA_PATH}/{imgname}.jpg"
        labels = f.readlines()
        # list返回目标index
        labels = labels[find_match(labels, img_path)]
        # 去除开头的路径和结尾的回车
        labels = labels[:-1].split(' ')[1:]   
        # print(labels) 
        for label in labels:
            label = [int(x) for x in label.split(',')]
            # label结构为[Xmin, Ymin, Xmax, Ymax, class]
            # print(label)
            pt1, pt2 = (label[0], label[1]), (label[2], label[3])
            cv2.putText(img, classes[label[4]], pt1, cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255))
            cv2.rectangle(img, pt1, pt2, (0,0,255,2))
        cv2.imshow("img", img)
        cv2.waitKey(0)

# 没有匹配项会error
def find_match(labels, target):
    print(target)
    for (i, label) in enumerate(labels):
        if(label.startswith(target)):
            return i
    if i == (len(labels)-1):
        print("\n --------find_match(): imgName error--------- \n")


show_labels_img('000009')